package com.luocj.refresh.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luocj.refresh.R;
import com.luocj.refresh.adapter.GridItemAdapter;
import com.luocj.refresh.model.MultiModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

public class GrideItemActivity extends AppCompatActivity {

    private SmartRefreshLayout smartrefreshlayout;
    private RecyclerView recyclerView;
    private Context mContext;
    private GridItemAdapter adapter;
    private String TAG = GrideItemActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gride_item);
        mContext = this;
//        initData();
        initView();
    }

    private ArrayList<MultiModel> initData() {
        ArrayList<MultiModel> models = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
//            if (i % 2 == 0) {
//                models.add(new MultiModel("title:" + i, "content" + i, 1));
//            }else {
//                models.add(new MultiModel("title:" + i, "content" + i, 3));
//            }

            models.add(new MultiModel("title:" + i, "content" + i, (int) (Math.random() * 2 + 1)));
            Log.i(TAG, "initData: " + models.get(i).toString());
        }

        return models;
    }

    private void initView() {
        smartrefreshlayout = findViewById(R.id.smartrefreshlayout_gride_item);
        recyclerView = findViewById(R.id.recyclerview_gride_item);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        adapter = new GridItemAdapter(mContext, initData());
        adapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                int spanSize = adapter.getData().get(position).getSpanSize();
                return spanSize;
            }
        });
        recyclerView.setAdapter(adapter);
    }
}
